package contacts.view;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import contacts.controller.ContactsControllerListener;
import contacts.po.Room;

public class RoomDialog extends JDialog {
	private static final long serialVersionUID = -2267052160715466957L;
	private ContactsControllerListener contactsControllerListener;
	private Room room;
	private JPanel roomAddPanel;
	private JTextField roomNoTextField;
	private JTextField roomNameTextField;
	private JButton confirmButton;

	public RoomDialog(ContactsFrame owner, String title, Room room) {
		this.contactsControllerListener = owner.getControllerListener(); // 让contactsFrame和roomAddDialog共用同一个controller对象
		this.contactsControllerListener.setRoomAddDialog(this);
		this.setTitle(title);
		this.room = room;
		this.add(getRoomAddPanel());
		this.setModal(true); // 设有模
		this.setSize(620, 260);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
		this.validate();
	}

	private JPanel getRoomAddPanel() {
		roomAddPanel = new JPanel();
		roomAddPanel.setBackground(Color.orange);
		if ("添加宿舍".equals(this.getTitle())) {
			roomNoTextField = new JTextField(12);
			roomNameTextField = new JTextField(12);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addRoomPost"); // 添加宿舍
		} else {
			roomNoTextField = new JTextField(String.valueOf(room.getRoomNo()), 20);
			roomNameTextField = new JTextField(room.getRoomName(), 20);
		}
		confirmButton.addActionListener(contactsControllerListener);
		roomAddPanel.add(new JLabel("宿舍号"));
		roomAddPanel.add(roomNoTextField);
		roomAddPanel.add(new JLabel("宿舍名称"));
		roomAddPanel.add(roomNameTextField);
		roomAddPanel.add(confirmButton);
		return roomAddPanel;
	}

	public JTextField getRoomNoTextField() {
		return roomNoTextField;
	}

	public void setRoomNoTextField(JTextField roomNoTextField) {
		this.roomNoTextField = roomNoTextField;
	}

	public JTextField getRoomNameTextField() {
		return roomNameTextField;
	}

	public void setRoomNameTextField(JTextField roomNameTextField) {
		this.roomNameTextField = roomNameTextField;
	}
}
